package top.huxy.boot.controller.admin;

import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.*;
import top.huxy.boot.annotation.Master;
import top.huxy.boot.annotation.Operation;
import top.huxy.boot.annotation.RequestRemake;
import top.huxy.boot.model.entity.Blog;
import top.huxy.boot.service.impl.BackupServiceImpl;
import top.huxy.boot.util.obj.Result;

import java.util.List;

/**
 * @Classname BackupController
 * @Description BackupController
 * @Date 2021/10/5 14:39
 * @Created by huxy
 */
@RestController
@RequestMapping("/admin/backup")
public class BackupController {
    BackupServiceImpl backupService;

    public BackupController(BackupServiceImpl backupService) {
        this.backupService = backupService;
    }

    @GetMapping("/getBlogBackup")
    @Master
    @RequestRemake("获取博客备份")
    public Result getBlogBackup() {
        return Result.success(backupService.getBlogBackup());
    }

    @PostMapping("/restoreBackup")
    @Master
    @Operation("恢复备份")
    public Result restoreBackup(@RequestParam("list") String str) {
        List<Blog> blogList;
        try {
            blogList = JSON.parseArray(str, Blog.class);
        } catch (Exception e) {
            return Result.clientError("解析数据失败");
        }

        backupService.restoreBackup(blogList);
        return Result.success("ok");
    }
}
